/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package taskgraph.tasks.text;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import taskgraph.Config;
import taskgraph.ports.CharInputPort;
import taskgraph.ports.CharOutputPort;
import taskgraph.tasks.PrimitiveTask;


/**
 * Text replacement task.
 * 
 * Replaces text by combining regular expression search and
 * substring substitution.
 *  
 * @author Armando Blancas
 * @see taskgraph.ports.CharInputPort
 * @see taskgraph.ports.CharOutputPort
 */
public class ReplaceText extends PrimitiveTask {

	private static final int DEFAULT_CAPACITY = Config.get().bufferCapacity();
	
	private CharInputPort input;
	private CharOutputPort output;
	private String pattern;
	private String value;
	private int bufferSize = DEFAULT_CAPACITY;

	/**
	 * This constructor allows creating instances as beans.
	 */
    public ReplaceText() {
    	// do nothing
    }

	/**
     * Creates a fully configured instance.
     * 
     * @param input The char input port.
     * @param output The char output port.
     * @param pattern The pattern to search for.
     * @param value The replacement value.
     */
    public ReplaceText(final CharInputPort input, 
    		           final CharOutputPort output, 
    		           final String pattern,
    		           final String value) {
    	setInput(input);
    	setOutput(output);
    	setPattern(pattern);
    	setValue(value);
    }

	/**
	 * Gets the buffer size for reading chunks of characters.
	 * 
	 * @return the bufferSize
	 */
	public int getBufferSize() {
		return bufferSize;
	}

    /**
     * Gets the input port.
     * 
	 * @return The char input port.
	 */
	public CharInputPort getInput() {
		return input;
	}

    /**
     * Gets the output port.
     * 
	 * @return The char output port.
	 */
	public CharOutputPort getOutput() {
		return output;
	}
	
    /**
	 * Gets the pattern for grep.
	 * 
	 * @return The {@code String} pattern value.
	 */
	public String getPattern() {
		return pattern;
	}

    /**
	 * Gets the value that replaces the pattern to search.
	 * 
	 * @return The {@code String} value for substitution..
	 */
	public String getValue() {
		return value;
	}
	
	/**
	 * Performs the work of this task.
	 * 
	 * Reads lines from the input port, greps the pattern and if found 
	 * replaces it with the new value. Writes all lines to the output port
	 * whether or not there was a substitution.
	 *<pre>
	 *while not EOF
	 *    read a line from input port
	 *    grep with given pattern
	 *    if found, replace it with new value
	 *    print the line regardless of change
	 *close input port
	 *close output port
	 *</pre>
	 */
	@Override
    public void run() {
    	try {
	        Pattern p = Pattern.compile(pattern);
    		while (!Thread.currentThread().isInterrupted()) {
    			String line = input.readLine();
    			if (line == null) {
    				break;
    			}
    	        Matcher m = p.matcher(line);
	        	output.println(m.replaceAll(value));
            }
        } catch (InterruptedIOException ie) {
        	// interruption request
        } catch (IOException ioe) {
        	// pipe synch error
            log.error("ReplaceText#run()", ioe);
        } finally {
			input.close();
			output.close();
        }
    }

	/**
	 * Sets the buffer size for reading chunks of characters.
	 * <p>The argument should not be less than the default. If it is, the
	 * default size is used instead.
	 * 
	 * @param bufferSize the bufferSize to set
	 */
	public void setBufferSize(final int bufferSize) {
		this.bufferSize = 
			(bufferSize < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY : bufferSize;
	}

	/**
	 * Sets the input port.
	 * 
	 * @param input The char input port.
	 */
    public void setInput(final CharInputPort input) {
    	if (input == null)
    		throw new IllegalArgumentException("input == null");
 		this.input = input;
	}

	/**
	 * Sets the output port.
	 * 
	 * @param output The char output port.
	 */
    public void setOutput(final CharOutputPort output) {
    	if (output == null)
    		throw new IllegalArgumentException("output == null");
 		this.output = output;
	}

	/**
	 * Sets the pattern to grep with.
	 * 
	 * @param pattern The {@code String} pattern to set.
	 */
	public void setPattern(final String pattern) {
    	if (pattern == null)
    		throw new IllegalArgumentException("pattern == null");
		this.pattern = pattern;
	}

	/**
	 * Sets the new value for occurrences of the search pattern.
	 * 
	 * @param value The {@code String} value for substitution.
	 */
	public void setValue(final String value) {
    	if (value == null)
    		throw new IllegalArgumentException("value == null");
		this.value = value;
	}

}
